﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PMSGSC.DAL;
using PMSGSC.BLL;
using DevExpress.XtraEditors;

namespace PMSGSC.App
{
    public partial class ucLoadWorkOrder : UserControl
    {
        public ucLoadWorkOrder()
        {
            InitializeComponent();
        }
        public bool isChanges;
        public void SetValue(string code, string name, string id)
        {
            txtCode.Text = code;
            txtName.Text = name;
            lbid.Text = id;
        }

        private void txtCode_Validating(object sender, CancelEventArgs e)
        {
            if (lbid.Text == "")
            {
                if (txtCode.Text != "  /  /     ")
                {
                    WorkOrder workOrder = WorkOrder_BLL.GetDataByNumber(txtCode.Text);
                    if (null == workOrder)
                    {
                        XtraMessageBox.Show("Component not exits!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        e.Cancel = true;
                    }
                    else
                    {
                        txtCode.Text = workOrder.WorkOrderNumber;
                        txtName.Text = workOrder.Title;
                        lbid.Text = workOrder.WorkOrder_ID.ToString();
                    }
                }
                else
                {
                    txtCode.Text = "";
                    txtName.Text = "";
                    lbid.Text = "";
                }
            }
        }
        private void ValidatingCode()
        {
            if (txtCode.Text != "    /  /     ")
            {
                WorkOrder workOrder = WorkOrder_BLL.GetDataByNumber(txtCode.Text);
                if (null == workOrder)
                {
                    XtraMessageBox.Show("Component not exits!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                else
                {
                    txtCode.Text = workOrder.WorkOrderNumber;
                    txtName.Text = workOrder.Title;
                    lbid.Text = workOrder.WorkOrder_ID.ToString();
                }
            }
            else
            {
                ValidatingName();
                txtCode.Text = "";
                txtName.Text = "";
                lbid.Text = "";
            }
        }
        public string type;
        private void txtName_Validating(object sender, CancelEventArgs e)
        {
            if (lbid.Text == "")
            {
                if (txtName.Text != "")
                {
                    WorkOrder workOrder = WorkOrder_BLL.GetDataByName(txtName.Text, frmMain.ship.Ship_ID);
                    if (null == workOrder)
                    {
                        XtraMessageBox.Show("Component not exits!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        e.Cancel = true;
                    }
                    else
                    {
                        txtCode.Text = workOrder.WorkOrderNumber;
                        txtName.Text = workOrder.Title;
                        lbid.Text = workOrder.WorkOrder_ID.ToString();
                    }
                }
                else
                {
                    txtCode.Text = "";
                    txtName.Text = "";
                    lbid.Text = "";
                }
            }
        }
        private void ValidatingName()
        {
            if (lbid.Text == "")
            {
                if (txtName.Text != "")
                {
                    WorkOrder workOrder = WorkOrder_BLL.GetDataByName(txtName.Text, frmMain.ship.Ship_ID);
                    if (null == workOrder)
                    {
                        XtraMessageBox.Show("Component not exits!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else
                    {
                        txtCode.Text = workOrder.WorkOrderNumber;
                        txtName.Text = workOrder.Title;
                        lbid.Text = workOrder.WorkOrder_ID.ToString();
                    }
                }
                else
                {
                    txtCode.Text = "";
                    txtName.Text = "";
                    lbid.Text = "";
                }
            }
        }

        private void txtName_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmGridWorkOrders workorders = new frmGridWorkOrders(-1);
            workorders.ShowDialog();
            if (null != workorders.WorkOrder)
            {
                txtCode.Text = workorders.WorkOrder.WorkOrderNumber;
                txtName.Text = workorders.WorkOrder.Title;
                lbid.Text = workorders.WorkOrder.WorkOrder_ID.ToString();
                isChanges = true;
            }
        }
    }
}
